//
//  XHBackRuleViewController.m
//  umibus
//
//  Created by fengyn on 16/6/29.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHBackRuleViewController.h"

#import "XHBackRuleCell.h"
#import "Services.h"

#import <DateTools/DateTools.h>

@interface XHBackRuleViewController ()
@property (weak, nonatomic) IBOutlet UIView *ui_borderView;
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSDictionary *m_backRuleDic;
@end

@implementation XHBackRuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.ui_borderView.layer.masksToBounds=YES;
    self.ui_borderView.layer.cornerRadius=13;
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHBackRuleCell" bundle:nil] forCellReuseIdentifier:XHBackRuleCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadBackRuleData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loadBackRuleData{
    [ServiceManager getBackRuleSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            self.m_backRuleDic=data;
            [self.ui_tableView reloadData];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (IBAction)closeButtonAction:(id)sender {
    [self dismissViewControllerAnimated:NO completion:^{
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows=self.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=nil;
    cell=[self configCellWithIndexPath:indexPath];
    return cell;
}

- (UITableViewCell *)configCellWithIndexPath:(NSIndexPath *)indexPath{
    XHBackRuleCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHBackRuleCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSInteger start=[self.startDate month];
    int day=15;
    if (self.m_backRuleDic) {
        day=[self.m_backRuleDic[@"dictDataValue"] intValue];
    }
    if (indexPath.row==0) {
        cell.ui_titleLabel.text=[NSString stringWithFormat:@"%ld月充值日期:%@",(long)start,[self.startDate formattedDateWithFormat:@"M月d日"]];
    }else{
        NSInteger mouth=start+indexPath.row;
        if (mouth>12) {
            NSInteger startYear=[self.startDate year];
            NSInteger m=((mouth-1)%12)+1;
            NSInteger y=((mouth-1)/12);
            cell.ui_titleLabel.text=[NSString stringWithFormat:@"%ld年%ld月充值日期:%ld月%d日",(startYear+y),m,m,day];
        }else{
        cell.ui_titleLabel.text=[NSString stringWithFormat:@"%ld月充值日期:%ld月%d日",mouth,mouth,day];
        }
    }
    
    cell.ui_detailLabel.text=[NSString stringWithFormat:@"￥%.2f",self.amount];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=25;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
